#!/bin/bash
VERSION=3.0
if [ -f /boot/firmware/PPPwn/config.sh ]; then
    source /boot/firmware/PPPwn/config.sh
fi

if [ -z $INTERFACE ]; then INTERFACE="eth0"; fi
if [ -z $FIRMWAREVERSION ]; then FIRMWAREVERSION="11.00"; fi
if [ -z $SHUTDOWN ]; then SHUTDOWN=true; fi
if [ -z $PPPOECONN ]; then PPPOECONN=false; fi
if [ -z $VMUSB ]; then VMUSB=false; fi
if [ -z $PPDBG ]; then PPDBG=true; fi
if [ -z $TIMEOUT ]; then TIMEOUT="2m"; fi
if [ -z $XFWAP ]; then XFWAP="1"; fi
if [ -z $XFGD ]; then XFGD="4"; fi
if [ -z $XFBS ]; then XFBS="0"; fi
if [ -z $XFNWB ]; then XFNWB=false; fi
if [ -z $OIPV ]; then OIPV=false; fi

if [ $OIPV = true ] ; then
XFIP="fe80::4141:4141:4141:4141"
else
XFIP="fe80::9f9f:41ff:9f9f:41ff"
fi


if [ $XFNWB = true ] ; then
    XFNW="--no-wait-padi"
else
    XFNW=""
fi

WKYTYP=$(tr -d '\0' </proc/device-tree/model)
if [[ $WKYTYP == *"Hisilicon"* ]] ;then
    sudo /boot/firmware/PPPwn/blue_on
    coproc read -t 5 && wait "$!" || true
    CPPBIN="pppwn7"
    VMUSB=false
else
    coproc read -t 5 && wait "$!" || true
    CPPBIN="pppwn64"
    VMUSB=false
fi
arch=$(getconf LONG_BIT)
if [ $arch -eq 32 ] && [ $CPPBIN = "pppwn64" ] ; then
    CPPBIN="pppwn7"
    VMUSB=false
fi
echo -e "\n\n\033[36m _____  _____  _____
|  __ \\|  __ \\|  __ \\
| |__) | |__) | |__) |_      ___ __
|  ___/|  ___/|  ___/\\ \\ /\\ / / '_ \\
| |    | |    | |     \\ V  V /| | | |
|_|    |_|    |_|      \\_/\\_/ |_| |_|\033[0m
\n\033[33mhttps://github.com/TheOfficialFloW/PPPwn\nhttps://github.com/xfangfang/PPPwn_cpp\033[0m\n" | sudo tee /dev/tty1

echo -e "\033[92mVersion $VERSION \033[0m" | sudo tee /dev/tty1
sudo systemctl stop pppoe >/dev/null 2>&1 &
if [ $VMUSB = true ] ; then
    echo "USB waiting....."
    coproc read -t 3 && wait "$!" || true
    sudo ip link set $INTERFACE up
else
    sudo ip link set $INTERFACE down
    coproc read -t 5 && wait "$!" || true
    sudo ip link set $INTERFACE up
fi
echo -e "\n\033[36m$WKYTYP\033[92m\nFirmware:\033[93m $FIRMWAREVERSION\033[92m\nInterface:\033[93m $INTERFACE\033[0m" | sudo tee /dev/tty1
echo -e "\033[92mPPPwn:\033[93m C++ $CPPBIN \033[0m" | sudo tee /dev/tty1


echo -e "\033[92mInternet Access:\033[93m Disabled\033[0m" | sudo tee /dev/tty1

if [ -f /boot/firmware/PPPwn/pwn.log ]; then
    sudo rm -f /boot/firmware/PPPwn/pwn.log
fi

if [[ ! $(ethtool $INTERFACE) == *"Link detected: yes"* ]]; then
    echo -e "\033[31mWaiting for link\033[0m" | sudo tee /dev/tty1
    while [[ ! $(ethtool $INTERFACE) == *"Link detected: yes"* ]]
    do
        coproc read -t 2 && wait "$!" || true
    done
    echo -e "\033[32mLink found\033[0m\n" | sudo tee /dev/tty1
fi

WKYIP=$(hostname -I) || true
if [ "$WKYIP" ]; then
    echo -e "\n\033[92mIP: \033[93m $WKYIP\033[0m" | sudo tee /dev/tty1
fi
echo -e "\n\033[95mReady for console connection\033[0m\n" | sudo tee /dev/tty1

#系统版本检测
if [[ $FIRMWAREVERSION == "7.00" ]] || [[ $FIRMWAREVERSION == "7.01" ]] || [[ $FIRMWAREVERSION == "7.02" ]] ;then
    STAGEVER="7.00"
    elif [[ $FIRMWAREVERSION == "7.50" ]] || [[ $FIRMWAREVERSION == "7.51" ]] || [[ $FIRMWAREVERSION == "7.55" ]] ;then
    STAGEVER="7.50"
    elif [[ $FIRMWAREVERSION == "8.00" ]] || [[ $FIRMWAREVERSION == "8.01" ]] || [[ $FIRMWAREVERSION == "8.03" ]] ;then
    STAGEVER="8.00"
    elif [[ $FIRMWAREVERSION == "8.50" ]] || [[ $FIRMWAREVERSION == "8.52" ]] ;then
    STAGEVER="8.50"
    elif [[ $FIRMWAREVERSION == "9.00" ]] ;then
    STAGEVER="9.00"
    elif [[ $FIRMWAREVERSION == "9.03" ]] || [[ $FIRMWAREVERSION == "9.04" ]] ;then
    STAGEVER="9.03"
    elif [[ $FIRMWAREVERSION == "9.50" ]] || [[ $FIRMWAREVERSION == "9.51" ]] || [[ $FIRMWAREVERSION == "9.60" ]] ;then
    STAGEVER="9.50"
    elif [[ $FIRMWAREVERSION == "10.00" ]] || [[ $FIRMWAREVERSION == "10.01" ]] ;then
    STAGEVER="10.00"
    elif [[ $FIRMWAREVERSION == "10.50" ]] || [[ $FIRMWAREVERSION == "10.70" ]] || [[ $FIRMWAREVERSION == "10.71" ]] ;then
    STAGEVER="10.50"
    elif [[ $FIRMWAREVERSION == "11.00" ]] ;then
    STAGEVER="11.00"
else
    echo -e "\033[31m\nIncorrect firmware value: $FIRMWAREVERSION \033[0m\n" | sudo tee /dev/tty1
    exit 1
fi

while  true; do 
    while read -r stdo ;
    do
        echo -e $stdo | sudo tee /dev/tty1 | sudo tee /dev/pts/* | sudo tee -a /boot/firmware/PPPwn/pwn.log
        if [[ $stdo  == *"Done!"* ]] ; then
            #成功，亮绿灯 
            if [[ $WKYTYP == *"Xunlei OneCloud"* ]] ;then
              sudo /boot/firmware/PPPwn/green_on
            fi
            echo -e "\033[32m\nConsole PPPwned! \033[0m\n" | sudo tee /dev/tty1
            coproc read -t 2 && wait "$!" || true
            if [ $PPPOECONN = true ] ; then
                sudo systemctl start pppoe  /dev/null 2>&1 &
            else
                if [ $SHUTDOWN = true ] ; then
                    coproc read -t 5 && wait "$!" || true
                    sudo poweroff
                else
                    if [ $VMUSB = true ] ; then
                        sudo systemctl start pppoe
                    else
                        coproc read -t 2 && wait "$!" || true
                       sudo ip link set $INTERFACE down
                    fi
                fi
            fi
            exit 0
            elif [[ $stdo  == *"Scanning for corrupted object...failed"* ]] ; then
            echo -e "\033[31m\nFailed retrying...\033[0m\n" | sudo tee /dev/tty1
            elif [[ $stdo  == *"Unsupported firmware version"* ]] ; then
            echo -e "\033[31m\nUnsupported firmware version\033[0m\n" | sudo tee /dev/tty1
            exit 1
            elif [[ $stdo  == *"Cannot find interface with name of"* ]] ; then
            echo -e "\033[31m\nInterface $INTERFACE not found\033[0m\n" | sudo tee /dev/tty1
            exit 1
        fi
    done < <(timeout $TIMEOUT sudo /boot/firmware/PPPwn/$CPPBIN --interface "$INTERFACE" --fw "${FIRMWAREVERSION//.}" --ipv "$XFIP" )
    coproc read -t 2 && wait "$!" || true
done
